/* 
    Purpose: Using the 1960-1990 Censuses,
             this file creates father income scores at 
             the race-only, south-only, occ x south, 
             and race x south levels. Income scores 
             are then merged (along with
             1940 income scores) by level of variation. 
    
    Note: Predicted income is missing in 1990 for a couple 
          cells where occupation=65, but as there are no 
          respondents in the eventual baseline sample 
          with retrospective father occupation =65, there's 
          no need to impute income.

    Creates: All output files have the naming structure
            "incomescores_fathers_1940_to1990_"
*/

clear
set more off
cd "$Mydirectory1/1_DataSources/CensusData/"


    foreach d in 6 7 8 9  {

        use ./output/Census19`d'0_fathers_ages30to50.dta, clear
        gen tag=1 
        gen number=1

*******************
*** TEMPLATES
*******************
    * Template 1: race
        preserve
            collapse (rawsum) tag, by(race)
            
            drop tag
            tempfile template_byrace
            save `template_byrace'
                
        restore 

    * Template 2: south
        preserve 
            collapse (rawsum) tag, by(south_merge)
            
            drop tag
            tempfile template_bysouth
            save `template_bysouth'
                
        restore 

    * Template 3: occ x south
        preserve
            collapse (rawsum) tag, by(fatheroccej)
                    
            expand 2, gen(south_merge)
            
            drop tag
            tempfile template_byocc_bys
            save `template_byocc_bys'
                
        restore 

    * Template 4: race x south
        preserve
            collapse (rawsum) tag, by(race)
            
            expand 2, gen(south_merge)
                    
            drop tag
            tempfile template_byr_bys
            save `template_byr_bys'
                
        restore 

*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*

        global income_measures "HHinc" 

*******************
*** COLLAPSE 
*******************

    foreach x in byrace bysouth byocc_bys byr_bys {

        if "`x'"=="byrace" local cell "race"
        if "`x'"=="bysouth" local cell "south_merge"
        if "`x'"=="byocc_bys" local cell "fatheroccej south_merge"
        if "`x'"=="byr_bys" local cell "race south_merge"

     * Weight
        local weight "perwt"

    preserve 

        summ fatheroccej 
        
        collapse (rawsum) number (mean) $income_measures [aw=`weight'], by(`cell') 
        
        foreach c in $income_measures {
        rename `c' avg_`c'_19`d'0_`x'
        label var avg_`c'_19`d'0_`x' "Coarse income score, average, 19`d'0 using `c'"
        }
        
        tempfile incomescores
        save `incomescores'

        use `template_`x''
        merge 1:1 `cell' using `incomescores', nogen
        
        replace number=0 if number==.
        label var number "Number of obs in cell"
        rename number number_19`d'0obs_`x'

        tempfile incomescores_`x'_19`d'0
        save `incomescores_`x'_19`d'0'
        
    restore 
            
    }

}

****************************************
* MERGE/SAVE (BY LEVEL OF VARIATION)
****************************************
    foreach x in byrace bysouth byocc_bys byr_bys {

        if "`x'"=="byrace" {
            local cell_merge "race"
            local x2 "byrace_only"
        }  
        if "`x'"=="bysouth" {
            local cell_merge "south_merge"
            local x2 "bysouth"
        }
        if "`x'"=="byocc_bys" {
            local cell_merge "fatheroccej south_merge"
            local x2 "byocc_bysouth"
        }
        if "`x'"=="byr_bys" {
            local cell_merge "race south_merge"
            local x2 "byrace_bysouth_only"  
        } 
        * Bring in 1940 income scores 
        use ./output/avgincomes_fathers1940_`x2'.dta, clear

        drop incwage fam_income
        
        foreach var of varlist hh_income {
            local lab_og: variable label `var'
            label variable `var' "`lab_og' (1940)"

            ren `var' `var'_`x'
        }

        merge 1:1 `cell_merge' using `incomescores_`x'_1960'
        assert _merge==3
        drop _merge

        merge 1:1 `cell_merge' using `incomescores_`x'_1970'
        assert _merge==3
        drop _merge

        merge 1:1 `cell_merge' using `incomescores_`x'_1980'
        assert _merge==3
        drop _merge

        merge 1:1 `cell_merge' using `incomescores_`x'_1990'
        assert _merge==3
        drop _merge

        compress
        save ./output/incomescores_fathers_1940_to1990_`x'.dta, replace
    }


